// import { login, forgetPassword, logout, getInfo } from '@/api/user'
import {
	getToken,
	setToken,
	removeToken,
	getLoc,
	setLoc,
	removeLoc
} from '@/utils/auth'
import {
	resetRouter
} from '@/router'
import Cookies from 'js-cookie'

var user = getLoc('user') ? getLoc('user') : {}
var lang = getLoc('lang') ? getLoc('lang') : 'zh';
var langStr = lang !== 'zh' ? '中文繁體' : 'Eng'

const state = {
	token: getToken(),
	name: '',
	avatar: '',
	lang: {
		langStr,
		loc: lang
	},
	coordinate: '',
	tabNavText: {},
	roles: user.roles,
	saveUserPasswords: user.username,
	username: user.username,
	password: user.password
}
console.log('loclang', user)
// console.log('21312',state,getLoc('user'))
const mutations = {
	SET_TOKEN: (state, token) => {
		state.token = token
	},
	SET_USER_PASSWORD: (state, user) => {
		state.username = user.username
		state.password = user.password

	},
	SET_LANG: (state, lang) => {
		state.lang = lang
		//state.langStr = lang.langStr
	},
	updateTabInfo: (state, info) => {
		state.tabNavText = info
	},
	// SET_NAME: (state, name) => {
	//   state.name = name
	// },
	// SET_AVATAR: (state, avatar) => {
	//   state.avatar = avatar
	// },
	// SET_USERTYPE: (state, userType) => {
	//   state.userType = userType
	// },
	SET_ROLES: (state, roles) => {
		state.roles = roles
		// state.avatar 
		// state.name 
		// state.userType 
	},
	GET_COORDINATE: (state, loc) => {
		state.coordinate = loc.coordinate
	}
}
const actions = {
	// user login
	langChange({
		commit
	}, lang) {

		var langObj = {}
		langObj.loc = lang === 'Eng' ? 'zh' : 'en',
			langObj.langStr = lang
		console.log("lang", langObj)
		this._vm.$vantLocales(langObj.loc)
		setLoc('lang', langObj.loc)

		commit('SET_LANG', langObj)
	},
	updateTabInfo({
		commit
	}, info = {}) {
		// this.vm.$H() info
		commit('updateTabInfo', info)
	},
	login({
		commit
	}, userInfo) {
		const {
			username,
			password
		} = userInfo
		return new Promise((resolve, reject) => {
			this._vm.$ajax({
				url: '/roll_call/signin',
				method: 'post',
				data: {
					member: username.trim(),
					password
				}
			}).then((response) => {
				console.log('sdas', response)
				const data = response
				user.roles = data.result.role
				commit('SET_TOKEN', data.result.token)
				commit('SET_ROLES', data.result.role)
				setToken(data.result.token)

				setLoc('user', user)
				resolve(response)
			}).catch((error) => reject(error))
		})
	},
	// user logout
	logout({
		commit,
		state
	}) {
		return new Promise((resolve, reject) => {
			this._vm.$ajax.post('roll_call/signout', {
				data: {
					Authorization: state.token
				}
			}, (res) => {
				commit('SET_TOKEN', '')
				commit('SET_ROLES', '')
				Cookies.set('userType', '')

				removeToken()
				//removeLoc('user')
				resetRouter()
				location.reload()
				resolve(res)
			}, (error) => {
				reject(error)
			})
		})
	},
	// '/forgot/send/email'
	forgetPassword({
		commit,
		state
	}, params) {
		return new Promise((resolve, reject) => {
			this._vm.$ajax.post('/forgot/send/email', {
				params
			}, (res) => {
				resolve(res)
			}, (error) => {
				reject(error)
			})
		})
	},
	resetPassword({
		commit,
		state
	}, params) {
		return new Promise((resolve, reject) => {
			this._vm.$ajax.post('/forgot/modify/paw', {
				params
			}, (res) => {
				resolve(res)
			}, (error) => {
				reject(error)
			})
		})
	},
	// remove token
	resetToken({
		commit
	}) {
		return new Promise(resolve => {
			commit('SET_TOKEN', '')
			removeToken()
			resolve()
		})
	},
	saveUserPasswords({
		commit
	}, obj) {
		commit('SET_USER_PASSWORD', obj)
		var {
			username,
			password,
			check
		} = obj
		//	console.log('sa',this,username,password,obj)
		user.username = username;
		user.password = password
		check ? setLoc('user', user) : removeLoc('user')
	},
	getLoction({
		commit
	}) {
		let that = {}
		if (navigator.geolocation) {
			navigator.geolocation.getCurrentPosition(function(position) {
				// 获取成功
				try {
					var latitude = position.coords.latitude;
					var longitude = position.coords.longitude;
					that.position = position
					that.coords = position.coords.accuracy
					that.coordinate = latitude + ',' + longitude;
					commit('GET_COORDINATE', that)
				} catch (err) {}
				console.log("spo", position)

			}, (err) => {
				if(this._vm.env.BASE_URL !=='/hkmobile/'){
					alert('获取失败' + JSON.stringify(err))
					console.log(err)
					if (err.code == 1) {
						return alert('用户拒绝页面发起的地理位置')
					}
					if (err.code == 2) {
						return alert('无法获取当前位置')
					}
					if (err.code == 3) {
						return alert('超时')
					}
					return alert('未知错误')
				}
				
			}, {
				enableHighAcuracy: false, //  是否启动高精模式（布尔值）
				maximumAge: 0, // 设置定位缓存过期的时间（毫秒，0为禁用缓存）
				timeout: 10000, // 设置获取定位信息的时常 （超时触发ErrorCallback）
			})
		} else {
			console.debug('不支持获取GPS地理位置');
		}
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
